<?php

namespace Es;

use Elasticsearch\ClientBuilder;
use Elasticsearch\Common\Exceptions\BadRequest400Exception;


class MyElasticsearch
{
    //ES客户端链接
    private static $client = null;

    /**
     * 构造函数
     */
    public function __construct()
    {

        if (!self::$client) {
            self::$client = ClientBuilder::create()->setHosts(['127.0.0.1'])->build();
        }

    }

    //单例模式
//    public  function instance()
//    {
//
//        if(empty($this->client))
//        {
//            $this->client = new MyElasticsearch();
//
//        }
//        return $this->client;
//    }

    /**
     * 判断索引值是否存在
     * 返回ture或false true索引已存在 false不存在
     */
    public function existsIndex($index_name)
    {

        $params = [
            'index' => $index_name,
        ];
        try {
            return self::$client->indices()->exists($params);
        } catch (BadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg, true);
            return $msg;
        }
    }

    /**
     * 创建索引
     * @param $index_name
     * @return array|mixed
     */
    public function create_index($index_name)
    {
        $params = [
            'index' => $index_name,
            'body' => [
                'settings' => [
                    'number_of_shards' => 5,
                    'number_of_replicas' => 0
                ]
            ]
        ];
        try {
            return self::$client->indices()->create($params);
        } catch (BadRequest400Exception $e) {
            $msg = $e->getMessage();
            $msg = json_decode($msg, true);
            return $msg;
        }
    }

    /**
     * 删除索引
     * @param $index_name
     * @return array
     */
    public function delete_index($index_name)
    {
        $params = ['index' => $index_name];
        $response = self::$client->indices()->delete($params);
        return $response;
    }

    /**
     * 添加文档
     * @param $id
     * @param $doc ['id'=>100,'title'=>'phone']
     * @param $index_name
     * @param $type_name
     * @return array|callable
     */
    public function add_doc($id, $doc, $index_name, $type_name)
    {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id,
            'body' => $doc
        ];
        $response = self::$client->index($params);
        return $response;
    }

    /**
     * 判断文档是否存在
     * @param $id
     * @param $index_name
     * @param $type_name
     * @return bool
     */
    public function exists_doc($id, $index_name, $type_name)
    {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
        $response = self::$client->exists($params);
        return $response;
    }

    /**
     * 获取文档
     * @param $id
     * @param $index_name
     * @param $type_name
     * @return array|callable
     */
    public function get_doc($id, $index_name, $type_name)
    {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
        $response = self::$client->get($params);
        return $response;
    }


    /**
     * 更新文档
     * @param $id
     * @param $index_name
     * @param $type_name
     * @param $body
     * @return array|callable
     */
    public function update_doc($id, $index_name, $type_name, $body)
    {
        //可以灵活添加新字段，最好不要乱添加
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id,
            'body' => $body
        ];
        $response = self::$client->update($params);
        return $response;
    }


    /**删除文档
     * @param $id
     * @param $index_name
     * @param $type_name
     * @return array|callable
     */
    public function delete_doc($id, $index_name, $type_name)
    {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'id' => $id
        ];
        $response = self::$client->delete($params);
        return $response;
    }

    /**
     * 搜索文档 (分页，排序，权重，过滤)
     * @param string $index_name
     * @param string $type_name
     * @param array $body
     * $body = [
     * 'query' => [
     * 'bool' => [
     * 'should' => [
     * [
     * 'match' => [
     * 'cate_name' => [
     * 'query' => $keywords,
     * 'boost' => 4, // 权重大
     * ]
     * ]
     * ],
     * [
     * 'match' => [
     * 'goods_name' => [
     * 'query' => $keywords,
     * 'boost' => 3,
     * ]
     * ]
     * ],
     * [
     * 'match' => [
     * 'goods_introduce' => [
     * 'query' => $keywords,
     * 'boost' => 2,
     * ]
     * ]
     * ]
     * ],
     * ],
     * ],
     * 'sort' => ['id'=>['order'=>'desc']],
     * 'from' => $from,
     * 'size' => $size
     * ];
     * @return array
     */

    public function search_doc($index_name, $type_name, $body)
    {
        $params = [
            'index' => $index_name,
            'type' => $type_name,
            'body' => $body
        ];
        $result = self::$client->search($params);
        return $result;
    }
}